package com.zby.business.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zby.business.R;
import com.zby.business.activity.MySalePage;
import com.zby.business.activity.Order;
import com.zby.business.activity.UpLoad;
import com.zby.business.util.App;
import com.zby.business.util.Util;

/**
 * Created by BY on 2015/12/29.
 */
public class MineFragment extends Fragment implements View.OnClickListener {
    private View view;
    private TextView userNameTextView;
    private LinearLayout myOrderLayout, toBePaidLayout, toBeShippedLayout, toBeReceivedLayout, toBeEvaluateLayout, afterSaleLayout;
    private TextView saleButton, users, manage;
    private App app;
    private FragmentTransaction ft;
    private FragmentManager fm;
    private ManageFragment manageFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_design, null);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        App app = (App) getActivity().getApplication();
        if (!app.getName().equals("")) {
            userNameTextView.setText(app.getName());
        }
        super.onResume();
    }

    private void initView() {
        fm = getFragmentManager();
        app = (App) getActivity().getApplication();
        myOrderLayout = (LinearLayout) view.findViewById(R.id.myOrderLayout);
        toBePaidLayout = (LinearLayout) view.findViewById(R.id.toBePaidLayout);
        toBeShippedLayout = (LinearLayout) view.findViewById(R.id.toBeShippedLayout);
        toBeReceivedLayout = (LinearLayout) view.findViewById(R.id.toBeReceivedLayout);
        toBeEvaluateLayout = (LinearLayout) view.findViewById(R.id.toBeEvaluateLayout);
        afterSaleLayout = (LinearLayout) view.findViewById(R.id.afterSaleLayout);
        userNameTextView = (TextView) view.findViewById(R.id.userNameTextView);
        saleButton = (TextView) view.findViewById(R.id.saleButton);
        users = (TextView) view.findViewById(R.id.users);
        manage = (TextView) view.findViewById(R.id.manage);
        saleButton.setOnClickListener(this);
        users.setOnClickListener(this);
        manage.setOnClickListener(this);
        myOrderLayout.setOnClickListener(this);
        toBePaidLayout.setOnClickListener(this);
        toBeShippedLayout.setOnClickListener(this);
        toBeReceivedLayout.setOnClickListener(this);
        toBeEvaluateLayout.setOnClickListener(this);
        afterSaleLayout.setOnClickListener(this);
        if (!app.getName().equals("admin")) {
            saleButton.setVisibility(View.INVISIBLE);
            users.setVisibility(View.INVISIBLE);
            manage.setVisibility(View.INVISIBLE);
        }
    }

    private void jumpToOrder(int position) {
        if (!app.getUserId().equals("29")) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(getContext(), Order.class);
            bundle.putInt("position", position);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {

        ft = fm.beginTransaction();
        switch (v.getId()) {
            case R.id.myOrderLayout:
                jumpToOrder(0);
                break;
            case R.id.toBePaidLayout:
                jumpToOrder(1);
                break;
            case R.id.toBeShippedLayout:
                jumpToOrder(2);
                break;
            case R.id.toBeReceivedLayout:
                jumpToOrder(3);

                break;
            case R.id.toBeEvaluateLayout:
                jumpToOrder(4);

                break;
            case R.id.afterSaleLayout:
                Util.loadingAlert(getContext());
                break;
            case R.id.saleButton:
                startActivity(new Intent(getActivity(), MySalePage.class));
                break;
            case R.id.users:
                startActivity(new Intent(getActivity(), UserInfo.class));
                break;
            case R.id.manage:
                if (manageFragment == null) {
                    manageFragment = new ManageFragment();
                    ft.add(manageFragment, "MANAGE");
                }
                ft.replace(R.id.mainFrame, manageFragment);
                ft.addToBackStack(null);
                break;
        }
        ft.commit();
    }
}
